<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/12 0012
 * Time: 上午 1:41
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class Book
{
    private $tableName = 'books';
    private $tableDB = null;
    private $error = 0;
    private $message = [
        0 => '无数据'
    ];

    public function __construct( $id = null )
    {
        $this->tableDB = DB::table( $this->tableName );

        if ( $id !== null )
        {
            $this->tableDB = $this->tableDB->where('id', '=', $id);
        }
    }

    public function getBooksByMaterialId( $materialId )
    {
        $result = $this->tableDB->where('material_id', '=', $materialId)->get();
        if ( $result )
        {
            return $result;
        }
        else
        {
            $this->error = 0;
            return null;
        }
    }

    public function getAllBooks()
    {
        $result = $this->tableDB->get();

        return $result;
    }

    public function getMessage()
    {
        return $this->message[ $this->error ];
    }
}